namespace DotNetNuke.Services.Vendors
{
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Data;
    using Microsoft.VisualBasic;
    using System;
    using System.Collections;
    using System.Data;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;

    public class VendorController
    {
        public int AddVendor(VendorInfo objVendor)
        {
            return DataProvider.Instance().AddVendor(objVendor.PortalId, objVendor.VendorName, objVendor.Unit, objVendor.Street, objVendor.City, objVendor.Region, objVendor.Country, objVendor.PostalCode, objVendor.Telephone, objVendor.Fax, objVendor.Cell, objVendor.Email, objVendor.Website, objVendor.FirstName, objVendor.LastName, objVendor.UserName, objVendor.LogoFile, objVendor.KeyWords, objVendor.Authorized.ToString());
        }

        public void DeleteVendor(int VendorID)
        {
            DataProvider.Instance().DeleteVendor(VendorID);
        }

        public void DeleteVendors([Optional, DefaultParameterValue(-1)] int PortalID)
        {
            IDataReader dr = DataProvider.Instance().GetVendors(PortalID, true, Null.NullInteger, Null.NullInteger);
            if (dr != null)
            {
                dr.NextResult();
            }
            while (dr.Read())
            {
                if (DateAndTime.DateDiff(DateInterval.Day, Convert.ToDateTime(RuntimeHelpers.GetObjectValue(dr["CreatedDate"])), DateAndTime.Now, FirstDayOfWeek.Sunday, FirstWeekOfYear.Jan1) > 7L)
                {
                    DataProvider.Instance().DeleteVendor(Convert.ToInt32(RuntimeHelpers.GetObjectValue(dr["VendorID"])));
                }
            }
            dr.Close();
        }

        public VendorInfo GetVendor(int VendorID, int PortalId)
        {
            return (VendorInfo) CBO.FillObject(DataProvider.Instance().GetVendor(VendorID, PortalId), typeof(VendorInfo));
        }

        public ArrayList GetVendors([Optional, DefaultParameterValue(-1)] int PortalId, [Optional, DefaultParameterValue("")] string Filter)
        {
            int TotalRecords=0;
            return this.GetVendorsByName(Filter, PortalId, 0, 0x186a0, ref TotalRecords);
        }

        public ArrayList GetVendors(int PortalId, bool UnAuthorized, int PageIndex, int PageSize, ref int TotalRecords)
        {
            IDataReader dr = DataProvider.Instance().GetVendors(PortalId, UnAuthorized, PageIndex, PageSize);
            ArrayList retValue = null;
            try
            {
                while (dr.Read())
                {
                    TotalRecords = Convert.ToInt32(RuntimeHelpers.GetObjectValue(dr["TotalRecords"]));
                }
                dr.NextResult();
                retValue = CBO.FillCollection(dr, typeof(VendorInfo));
            }
            finally
            {
                if (dr != null)
                {
                    dr.Close();
                }
            }
            return retValue;
        }

        public ArrayList GetVendorsByEmail(string Filter, int PortalId, int Page, int PageSize, ref int TotalRecords)
        {
            ArrayList GetVendorsByEmail;
            IDataReader dr = DataProvider.Instance().GetVendorsByEmail(Filter, PortalId, Page, PageSize);
            try
            {
                while (dr.Read())
                {
                    TotalRecords = Convert.ToInt32(RuntimeHelpers.GetObjectValue(dr["TotalRecords"]));
                }
                dr.NextResult();
                GetVendorsByEmail = CBO.FillCollection(dr, typeof(VendorInfo));
            }
            finally
            {
                if (dr != null)
                {
                    dr.Close();
                }
            }
            return GetVendorsByEmail;
        }

        public ArrayList GetVendorsByName(string Filter, int PortalId, int Page, int PageSize, ref int TotalRecords)
        {
            ArrayList GetVendorsByName;
            IDataReader dr = DataProvider.Instance().GetVendorsByName(Filter, PortalId, Page, PageSize);
            try
            {
                while (dr.Read())
                {
                    TotalRecords = Convert.ToInt32(RuntimeHelpers.GetObjectValue(dr["TotalRecords"]));
                }
                dr.NextResult();
                GetVendorsByName = CBO.FillCollection(dr, typeof(VendorInfo));
            }
            finally
            {
                if (dr != null)
                {
                    dr.Close();
                }
            }
            return GetVendorsByName;
        }

        public void UpdateVendor(VendorInfo objVendor)
        {
            DataProvider.Instance().UpdateVendor(objVendor.VendorId, objVendor.VendorName, objVendor.Unit, objVendor.Street, objVendor.City, objVendor.Region, objVendor.Country, objVendor.PostalCode, objVendor.Telephone, objVendor.Fax, objVendor.Cell, objVendor.Email, objVendor.Website, objVendor.FirstName, objVendor.LastName, objVendor.UserName, objVendor.LogoFile, objVendor.KeyWords, objVendor.Authorized.ToString());
        }
    }
}

